#include "Quad.h"
#include "DinoMath.h"
#include "GLES2\gl2.h"

Quad::Quad(void)
{
	m_one = DinoMath::vec3(0.0f, 0.0f, 0.0f);
	m_two = DinoMath::vec3(0.0f, 0.0f, 0.0f);
	m_three = DinoMath::vec3(0.0f, 0.0f, 0.0f);
	m_four = DinoMath::vec3(0.0f, 0.0f, 0.0f);

	Distance = 0.0f;

	// initialize arrays
	m_vertexArray[0] = m_one.x;
	m_vertexArray[1] = m_one.y;
	m_vertexArray[2] = m_one.z;
	m_vertexArray[3] = 1.0f;

	m_vertexArray[4] = m_two.x;
	m_vertexArray[5] = m_two.y;
	m_vertexArray[6] = m_two.z;
	m_vertexArray[7] = 1.0f;

	m_vertexArray[8] = m_three.x;
	m_vertexArray[9] = m_three.y;
	m_vertexArray[10] = m_three.z;
	m_vertexArray[11] = 1.0f;

	m_vertexArray[12] = m_four.x;
	m_vertexArray[13] = m_four.y;
	m_vertexArray[14] = m_four.z;
	m_vertexArray[15] = 1.0f;

	m_texArray[0] = 0.0f;
	m_texArray[0] = 0.0f;
	m_texArray[0] = 0.0f;
	m_texArray[0] = 1.0f;
	m_texArray[0] = 1.0f;
	m_texArray[0] = 1.0f;
	m_texArray[0] = 1.0f;
	m_texArray[0] = 0.0f;

	// get sum of quad
	calcSum();
}

Quad::Quad(DinoMath::vec3 one, DinoMath::vec3 two, DinoMath::vec3 three, DinoMath::vec3 four)
{
	m_one = one;
	m_two = two;
	m_three = three;
	m_four = four;

	Distance = 0.0f;

	// initialize arrays
	m_vertexArray[0] = m_one.x;
	m_vertexArray[1] = m_one.y;
	m_vertexArray[2] = m_one.z;
	m_vertexArray[3] = 1.0f;

	m_vertexArray[4] = m_two.x;
	m_vertexArray[5] = m_two.y;
	m_vertexArray[6] = m_two.z;
	m_vertexArray[7] = 1.0f;

	m_vertexArray[8] = m_three.x;
	m_vertexArray[9] = m_three.y;
	m_vertexArray[10] = m_three.z;
	m_vertexArray[11] = 1.0f;

	m_vertexArray[12] = m_four.x;
	m_vertexArray[13] = m_four.y;
	m_vertexArray[14] = m_four.z;
	m_vertexArray[15] = 1.0f;

	m_texArray[0] = 0.0f;
	m_texArray[1] = 0.0f;
	m_texArray[2] = 0.0f;
	m_texArray[3] = 1.0f;
	m_texArray[4] = 1.0f;
	m_texArray[5] = 1.0f;
	m_texArray[6] = 1.0f;
	m_texArray[7] = 0.0f;

	// get sum of quad
	calcSum();
}



Quad::~Quad(void)
{
}

// return distance from quad to given point
float Quad::GetDistance(DinoMath::vec3 &pt)
{
	DinoMath::vec3 res;
	GetSum(res);

	return DinoMath::DistanceVec3(pt._array, res._array);
}

void Quad::calcSum()
{
	m_sum.x = (m_one.x + m_two.x + m_three.x + m_four.x) / 4.0f;
	m_sum.y = (m_one.y + m_two.y + m_three.y + m_four.y) / 4.0f;
	m_sum.z = (m_one.z + m_two.z + m_three.z + m_four.z) / 4.0f;
}

void Quad::GetSum(DinoMath::vec3 &result)
{
	result = m_sum;
}

void Quad::GLRender()
{
	glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, m_vertexArray);
	glEnableVertexAttribArray(0);

	glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, m_texArray);
	glEnableVertexAttribArray(1);

	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}